import 'package:flutter/material.dart';
import '../widgets/bottom_navigation.dart';
import 'anime_list_page.dart';
import 'search_page.dart';
import 'favorites_page.dart';
import 'settings_page.dart';
import 'package:provider/provider.dart';
import '../providers/anime_provider.dart';

/// 应用首页 - 主容器页面
class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _currentIndex = 0;

  final List<Widget> _pages = [
    const AnimeListPage(),   // 动漫列表页面
    const SearchPage(),      // 搜索页面
    const FavoritesPage(),   // 收藏页面
    const SettingsPage(),    // 设置页面
  ];

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      final animeProvider = Provider.of<AnimeProvider>(context, listen: false);
      
      // 优先从缓存初始化数据
      animeProvider.initializeData();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        index: _currentIndex,
        children: _pages,
      ),
      bottomNavigationBar: BottomNavigation(
        currentIndex: _currentIndex,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }
} 